package M

import (
	"time"
)

var msessiondata tmsession

//私有存储
type tmsession struct {
	Maxtime int64
	ids     map[string]manageSession
}
type manageSession struct {
	time int64
	data map[string]string
}

//对外开放
type Msession struct{}

func (session Msession) Init(maxTime int64) {
	if msessiondata.Maxtime == 0 {
		msessiondata = tmsession{
			Maxtime: maxTime,
			ids:     map[string]manageSession{},
		}
		go msessionCheck()
	}
}
func (session Msession) Set(sessionid, key, value string) error {
	println("设置")
	_, ok := msessiondata.ids[sessionid]
	if !ok {
		msessiondata.ids[sessionid] = manageSession{
			time: time.Now().Unix(),
			data: make(map[string]string),
		}
	}
	msessiondata.ids[sessionid].data[key] = value
	return nil
}
func (session Msession) Get(sessionid, key string) string {
	println("读取")
	value, ok := msessiondata.ids[sessionid].data[key]
	if ok {
		return value
	}
	return ""
}
func (session Msession) Del(sessionid, key string) error {
	delete(msessiondata.ids[sessionid].data, key)
	return nil
}
func msessionCheck() {
	// tiker := time.NewTicker(time.Second)
	// for {
	// 	fmt.Println(<-tiker.C)
	// }
}
